home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 June: Reference Library / Dev.CD Jun 99 RL Disk 1.toast / Technical Documentation / Develop / develop Issue 29 / develop Issue 29 code / Sending PostScript Files / SendPS Sample / CurrentPrinter.c next >
Encoding:
Text File  |  1996-02-12  |  5.9 KB  |  204 lines  |  [TEXT/MPS ]

  1. // Current Printer info sample code
  2. //
  3. // Dave Polaschek and David Hayward
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyright 1995 - 1996, Apple Computer,Inc
  8. //
  9. // Check the default printer under GX or old printing architecture.
  10. // Print out the name, entityType, and zone.
  11. //
  12. // This sample is provided as-is. It's guaranteed to work on my
  13. // mac this instant, and nothing more. It shows how to determine
  14. // the currently selected printer now. Since this isn't an officially
  15. // supported endeavor, things may change in the future, and this
  16. // snippet will be for naught.
  17. //
  18. //
  19. //    You may incorporate this sample code into your applications without
  20. //    restriction, though the sample code has been provided "AS IS" and the
  21. //    responsibility for its operation is 100% yours.  However, what you are
  22. //    not permitted to do is to redistribute the source as "DSC Sample Code"
  23. //    after having made changes. If you're going to re-distribute the source,
  24. //    we require that you make it clear in the source that the code was
  25. //    descended from Apple Sample Code, but that you've made changes.
  26. //
  27.  
  28. // Revision history
  29. //
  30. // davep    8/16/95    initial cut
  31. // davep    1/9/96    cleanup and add comments
  32. // davep    2/12/96    back-port to MPW with SC, add headers
  33.  
  34.  
  35. #include <stdio.h>
  36. #include <Types.h>
  37. #include <TextUtils.h>
  38. #include <Files.h>
  39. #include <Folders.h>
  40. #include <Resources.h>
  41. #include <Aliases.h>
  42. #include <Gestalt.h>
  43.  
  44. #define gestaltGXVersion            'qdgx'            // in PrintingManager.h (old) or GXPrinting.h (new)
  45. #define gestaltGXPrintingMgrVersion    'pmgr'            // in PrintingManager.h (old) or GXPrinting.h (new)
  46. #define gestaltAliasMgrAttr            'alis'            // in Aliases.h
  47.  
  48. #define printingResourceID            0xE000            // 0xE000 = -8192
  49.  
  50.  
  51. OSErr        GetCurrentPrinter(Str255 printer);
  52. OSErr        test(void);
  53. Boolean        gxInstalled(void);
  54. Boolean        validPrinter(void);
  55.  
  56.  
  57. /*------------------------------------------------------------------------------*\
  58.         This function returns true if QuickDraw GX printing is available
  59. \*------------------------------------------------------------------------------*/
  60. Boolean gxInstalled(void)
  61. {
  62.     long version;
  63.  
  64.     if (Gestalt(gestaltGXVersion, &version) == noErr)
  65.         if (Gestalt(gestaltGXPrintingMgrVersion, &version) == noErr)
  66.             return(true);
  67.     return(false);
  68. }
  69.  
  70.  
  71. /*------------------------------------------------------------------------------*\
  72.         This function returns the network entity of the currently 
  73.         selected printer, and as an added bonus, you get an error code, too!
  74. \*------------------------------------------------------------------------------*/
  75. OSErr GetCurrentPrinter(Str255 printer)
  76. {
  77.     StringHandle    theDriver;
  78.     Handle            thePrinter;
  79.     OSErr            theErr;
  80.     short            resFileRefnum;
  81.  
  82.     theDriver = GetString(printingResourceID);
  83.     if (!theDriver) goto BADDriver;
  84.  
  85.     if (gxInstalled()) {
  86.         short    vRefNum;
  87.         long    dirID;
  88.         FSSpec    theFile;
  89.         long    resSize;
  90.  
  91.         theErr=FindFolder(kOnSystemDisk,kDesktopFolderType,kDontCreateFolder,&vRefNum,&dirID);
  92.         if (theErr != noErr) goto BADDriver;
  93.  
  94.         theErr=FSMakeFSSpec(vRefNum,dirID,*theDriver,&theFile);
  95.         if (theErr != noErr) goto BADDriver;
  96.  
  97.         resFileRefnum = FSpOpenResFile(&theFile,fsRdPerm);
  98.         if (resFileRefnum == -1) goto BADResFile;
  99.  
  100.         thePrinter = Get1Resource('comm', 0);
  101.         if (!thePrinter) goto BADPrinter;
  102.  
  103.         if (*((long *)*thePrinter) != 'PPTL') goto BADPrinter;
  104.  
  105.         resSize = GetHandleSize(thePrinter)-6;
  106.         BlockMoveData((*thePrinter)+6,printer,resSize);
  107.     } else {
  108.         Boolean     aliasCallsPresent;
  109.         Boolean     aliasResourcePresent;
  110.         long        resSize,response;
  111.         AliasHandle    theAlias;
  112.         
  113.         aliasCallsPresent = ((Gestalt(gestaltAliasMgrAttr,&response) == noErr) &&
  114.             (response & 1));
  115.  
  116.         theAlias = (AliasHandle) GetResource('alis',printingResourceID);
  117.         aliasResourcePresent = !!(theAlias);
  118.         
  119.         if (aliasCallsPresent && aliasResourcePresent) {
  120.             FSSpec    fileSpec;
  121.             Boolean    wasChanged;
  122.  
  123.             theErr = ResolveAlias(NULL, theAlias,&fileSpec,&wasChanged);
  124.             if (theErr != noErr) goto BADDriver;
  125.  
  126.             resFileRefnum = FSpOpenResFile(&fileSpec,fsRdPerm);
  127.             if (resFileRefnum == -1) goto BADResFile;
  128.         } else {
  129.             resFileRefnum = OpenResFile(*theDriver);
  130.             if (resFileRefnum == -1) goto BADResFile;
  131.         }
  132.         thePrinter = Get1Resource('PAPA', printingResourceID);
  133.         if (!thePrinter) goto BADPrinter;
  134.         resSize = GetHandleSize(thePrinter);
  135.         BlockMoveData(*thePrinter,printer,resSize);
  136.     }
  137.     ReleaseResource(thePrinter);
  138.     CloseResFile(resFileRefnum);
  139.  
  140.     return(noErr);
  141.  
  142. // Error returns
  143. BADPrinter:
  144.     CloseResFile(resFileRefnum);
  145. BADResFile:
  146.     theErr = ResError();
  147. BADDriver:
  148.     return(theErr);
  149. }
  150.  
  151.  
  152. /*------------------------------------------------------------------------------*\
  153.         This function is called by the test harness. Doesn't do much except
  154.         prove that things work without crashing.
  155. \*------------------------------------------------------------------------------*/
  156. OSErr test(void)
  157. {
  158.     OSErr    testValue;
  159.     Str255    printerEntity;
  160.  
  161.     testValue = GetCurrentPrinter(printerEntity);
  162.     if (testValue)
  163.         return testValue;
  164.     else {
  165.         Str31            printer,type,zone;
  166.         unsigned char    *currentString;
  167.  
  168.         currentString = printerEntity;
  169.         BlockMove(currentString,printer,Length(currentString)+1);
  170.         p2cstr(printer);
  171.         
  172.         currentString += Length(currentString) + 1;
  173.         BlockMove(currentString,type,Length(currentString) + 1);
  174.         p2cstr(type);
  175.         
  176.         currentString += Length(currentString) + 1;
  177.         BlockMove(currentString,zone,Length(currentString) + 1);
  178.         p2cstr(zone);
  179.  
  180.         printf("Current Printer name = %s\n"
  181.                 "EntityType = %s\n"
  182.                 "Zone = %s\n",printer,type,zone);
  183.         return noErr;
  184.     }
  185. }
  186.  
  187.  
  188. /*------------------------------------------------------------------------------*\
  189.         This function tells if there's a valid printer selected (i.e. not
  190.         aimed at a driver that's since been deleted or no printer selected
  191.         since last System sw install) in the chooser
  192. \*------------------------------------------------------------------------------*/
  193. Boolean validPrinter(void)
  194. {
  195.     Str255    printerEntity;
  196.     
  197.     if (GetCurrentPrinter(printerEntity) != noErr)
  198.         return false;
  199.     else if (Length(printerEntity) == 0)
  200.         return false;
  201.     else
  202.         return true;
  203. }
  204.